'use strict';
const createConfig = require('uni-config-center')
const uniPay = require('uni-pay')
const uniID = require('uni-id')

const db = uniCloud.database()
exports.main = async (event, context) => {
	const uniPayConfig = createConfig({
		pluginId: 'uni-pay'
	})
	const {
		wxConfigMp,
		wxConfigApp,
		wxConfigH5,
		aliConfigMp,
		aliConfigApp,
		aliConfigH5,
		appleConfigApp,
		// aliConfigSandbox
	} = uniPayConfig.requireFile('config.js')
	let {
		provider,
		outTradeNo,
		lx,
		uniIdToken
	} = event

	console.log(provider)

	let url = '';
	if (lx == 1) {
		url = 'http://www.txzh.com.cn/api.php?act=orderinfo';
	}

	if (lx == 2) {
		url = 'http://www.txzh.com.cn/api.php?act=rechargeinfo';
	}

	const orderList = await uniCloud.httpclient.request(url, {
		method: 'GET',
		data: {
			ordersn: outTradeNo
		},
		contentType: 'json', // 指定以application/json发送data内的数据
		dataType: 'json' // 指定返回值为json格式，自动进行parse
	})

	if (orderList.data.paystatus != 0) {
		return {
			code: -2,
			msg: '订单状态不正确',
		}
	}
	var wxConfigApp1= {
		appId: 'wxf44fee41278c0b6f',
		mchId: '1526024561',
		key: 'sdf51sdf51sdf51ds5sdf1rewew1eee1',
	};
	
	let uniPayInstance,
		notifyUrl =
		`https://f3746608-aeb6-4e88-b274-54f5dc1ba857.bspapp.com/http/payment-notify/${provider}_${context.PLATFORM}`,
		openid,
		tradeType
	// notifyUrl为接收通知的云函数的url，务必替换为自己的通知地址，云函数Url化请参考：https://uniapp.dcloud.io/uniCloud/http
	switch (provider + '_' + context.PLATFORM) {
		case 'wxpay_mp-weixin':
			uniPayInstance = uniPay.initWeixin(wxConfigMp)
			openid = userInfo.wx_openid['mp-weixin']
			tradeType = 'JSAPI'
			break;
		case 'alipay_mp-alipay':
			uniPayInstance = uniPay.initAlipay(aliConfigMp)
			openid = userInfo.ali_openid
			break;
		case 'wxpay_app-plus':
			uniPayInstance = uniPay.initWeixin(wxConfigApp1)
			tradeType = 'APP'
			break;
		case 'wxpay_app':
		 
			uniPayInstance = uniPay.initWeixin(wxConfigApp1)
			tradeType = 'APP'
			break;
		case 'alipay_app':
			uniPayInstance = uniPay.initAlipay(aliConfigApp)
			break;
		case 'alipay_app-plus':
			uniPayInstance = uniPay.initAlipay(aliConfigApp)
			break;
		case 'wxpay_h5':
			uniPayInstance = uniPay.initWeixin(wxConfigH5)
			tradeType = 'NATIVE'
			break;
		case 'alipay_h5':
			uniPayInstance = uniPay.initAlipay(aliConfigH5)
			// uniPayInstance = uniPay.initAlipay(aliConfigSandbox)
			tradeType = 'NATIVE'
			break;
		case 'appleiap-plus':
			uniPayInstance = uniPay.initappleiapPayment(appleConfigApp)
			tradeType = 'APP'
			break;
		default:
			return {
				code: -1,
					msg: '参数错误' + provider + '_' + context.PLATFORM
			}
	}

	let orderInfo

	// 请填写对应平台接收通知云函数对应的云函数url化地址，云函数url化请参考：https://uniapp.dcloud.io/uniCloud/http
	try {
		// 获取支付信息
		orderInfo = await uniPayInstance.getOrderInfo({
			openid, // App端支付时不需要openid，传入个undefined也没有影响
			outTradeNo,
			totalFee: orderList.data.money,
			subject: orderList.data.subject,
			body: orderList.data.body,
			notifyUrl,
			tradeType,
			// attach: "%7B%22type%22%3A%22goods%22%7D"
		})
	} catch (e) {
	console.log(22222)
			console.log(e)
		return {
			code: -3,
			msg: e
		}
	}

	return {
		outTradeNo,
		orderInfo
	}
};